// app.js
App({
  onLaunch(options) {
    // 登录
    this.onLogin();
    const that = this;
    // 获取系统信息
    const systemInfo = wx.getSystemInfoSync();
    // 胶囊按钮位置信息
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
    // 导航栏高度 = 状态栏高度 + 44
    that.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
    that.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
    that.globalData.menuTop = menuButtonInfo.top;
    that.globalData.menuHeight = menuButtonInfo.height;
  },
  onLogin() {
    var that = this;
    if (!wx.getStorageSync('token') || wx.getStorageSync('token') == '' || wx.getStorageSync('token') == 'undefined') {
      const loginPromise = new Promise((resolve, reject) => {
      wx.login({
        success(res) {
          if (res.code) {
            wx.setStorageSync('code', res.code);
            // wx.showLoading({
            //   title: '登陆中',
            //   icon:"loading"
            // })
            wx.request({
              url: 'https://repair.zhaishu.store/api/login/getOpenids',
              method: "POST",
              data: {
                code: res.code
              },
              header: {
                "Content-Type": "application/json"
              },
              success: (result) => {
                wx.hideLoading()
                if (result.data.code == 0) {
                  wx.setStorageSync('openId', result.data.msg);
                  wx.request({
                    url: 'https://repair.zhaishu.store/api/login/registers',
                    method: "POST",
                    data: {
                      openId: result.data.msg
                    },
                    header: {
                      "Content-Type": "application/json"
                    },
                    success: (res) => {
                      wx.hideLoading()
                      if (res.data.code == 0) {
                        var headPortrait = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';
                        var nickName = '游客' + that.generateRandomString();

                        wx.setStorageSync('headPortrait', res.data.data.user.headPortrait || headPortrait);
                        wx.setStorageSync('nickName', res.data.data.user.nickName || nickName);
                        wx.setStorageSync('token', res.data.data.token);
                        wx.setStorageSync('userId', res.data.data.user.id);
                        resolve();
                      } else {
                        wx.showToast({
                          title: '系统错误，请重新登录',
                        })
                      }
                    }
                  })
                } else {
                  wx.showToast({
                    title: '系统错误，请重新登录',
                  })
                }
              }
            })
          }
        }
      })
    })
    that.globalData.loginPromise = loginPromise;
    }
  },

  generateRandomString(length = 10) {
    var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    let result = '';
    for (let i = 0; i < length; i++) {
      result += chars[Math.floor(Math.random() * chars.length)]; 
    }
    return result;
  },

  globalData: {
    userInfo: null,
    loginCode: null,
    version: '1.0.0',
    baseUrl: 'https://repair.zhaishu.store',
    laborDispute: true,
    essentialInformationnext: true,
    navBarHeight: 0, // 导航栏高度
    menuRight: 0, // 胶囊距右方间距（方保持左、右间距一致）
    menuTop: 0, // 胶囊距顶部间距
    menuHeight: 0, // 胶囊高度（自定义内容可与胶囊高度保证一致
  },
  // onShow() {
  //   if(wx.getStorageSync('token') == '' ||  wx.getStorageSync('token') == 'undefined') {
  //     this.onLogin();
  //   }
  // },
})
